package com.xss.ipctest;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import com.xss.ipctest.aidl.BookManagerActivity;
import com.xss.ipctest.binderpool.BinderPoolActivity;
import com.xss.ipctest.messenger.MessengerActivity;
import com.xss.ipctest.model.User;
import com.xss.ipctest.provider.ProviderActivity;
import com.xss.ipctest.socket.TCPClientActivity;
import com.xss.ipctest.utils.Constant;
import com.xss.ipctest.utils.MyUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

/**
 * IPC 实现方式
 * 1.Bundle
 * 2.文件共享：前者将序列化对象存入文件，后者从文件中读取反序列化数据
 * 3.Messenger
 * 4.AIDL-->Binder池
 * 5.ContentProvider
 * 6.socket
 */
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.button1_main).setOnClickListener(this);
        findViewById(R.id.button2_main).setOnClickListener(this);
        findViewById(R.id.button3_main).setOnClickListener(this);
        findViewById(R.id.button4_main).setOnClickListener(this);
        findViewById(R.id.button5_main).setOnClickListener(this);
        findViewById(R.id.button6_main).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.button1_main://Bundle实现IPC
                Constant.flag = 3;
                User user = new User(1, "Jack", true);
                Intent intent = new Intent();
//                intent.putExtra("user_data", (Parcelable) user);
                Bundle bundle = new Bundle();
                bundle.putParcelable("user_data", user);
                bundle.putString("other_data", "Here are other data");
                intent.putExtras(bundle);
                intent.setClass(MainActivity.this, SecondActivity.class);
                startActivity(intent);
                break;
            case R.id.button2_main://Messenger
                Intent intent_messenger = new Intent(this, MessengerActivity.class);
                startActivity(intent_messenger);
                break;
            case R.id.button3_main://AIDL
                Intent intent_AIDL = new Intent(this, BookManagerActivity.class);
                startActivity(intent_AIDL);
                break;
            case R.id.button4_main://ContentProvider
                Intent intent_provider = new Intent(this, ProviderActivity.class);
                startActivity(intent_provider);
                break;
            case R.id.button5_main://Binder池
                Intent intent_binderpool=new Intent(this, BinderPoolActivity.class);
                startActivity(intent_binderpool);
                break;
            case R.id.button6_main://socket
                Intent intent_socket=new Intent(this, TCPClientActivity.class);
                startActivity(intent_socket);
                break;
            default:
                break;
        }
    }

    /**
     * 文件共享实现IPC
     */
    @Override
    protected void onResume() {
        super.onResume();
        persistToFile();
    }

    //对象序列化保存到文件中
    private void persistToFile() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                User user = new User(2, "This is a File", true);
                File dir = new File(Constant.CHAPTER_2_PATH);
                if (!dir.exists()) {
                    dir.mkdirs();
                }
                File cacheFile = new File(Constant.CACHE_FILE_PATH);
                ObjectOutputStream objectOutputStream = null;
                try {
                    objectOutputStream = new ObjectOutputStream(new FileOutputStream(cacheFile));
                    objectOutputStream.writeObject(user);
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    MyUtils.close(objectOutputStream);
                }
            }
        }).start();
    }
}
